package com.epfresh.views.banner;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;


import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.Ad;

import java.util.ArrayList;
import java.util.List;


public class SliderBannerController {

    private Context context;
    private SliderBanner mSliderBanner;
    private InnerAdapter mBannerAdapter = new InnerAdapter();

    private View.OnClickListener mClickItemListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    };


    public SliderBannerController(Context context, SliderBanner sliderBanner) {
        this.context = context;
        mSliderBanner = sliderBanner;
        sliderBanner.setAdapter(mBannerAdapter);

    }

    public View.OnClickListener getmClickItemListener() {
        return mClickItemListener;
    }

    public void setmClickItemListener(View.OnClickListener mClickItemListener) {
        this.mClickItemListener = mClickItemListener;
    }


    public void play(List<Ad> list) {
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetInvalidated();
        if (list != null) {
            int size = list.size();
            if (size > 0) {
                mSliderBanner.setDotNum(size == 1 ? 0 : size);
                int count = mBannerAdapter.getCount() / size * size;

                int start = count > 50 * size ? 50 * size : count / size;
                mSliderBanner.beginPlay(start);
            }
        }
    }

    public void clear() {

        mBannerAdapter.setData(null);
        mBannerAdapter.notifyDataSetChanged();
    }

    public void notifyDataSetChanged() {
        mBannerAdapter.notifyDataSetChanged();
    }

    public int getCount() {
        return mBannerAdapter.getListSize();
    }

    public List<Ad> getList() {
        return mBannerAdapter.getList();
    }

    private class InnerAdapter extends BannerAdapter {
        private List<Ad> mDataList = new ArrayList<>();

        // private ImageLoader imageLoader;

        public void setData(List<Ad> data) {
            mDataList.clear();
            if (data != null)
                mDataList.addAll(data);
        }

        public Ad getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.get(getPositionForIndicator(position));
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.size() == 0) {
                return 0;
            }
            return position % mDataList.size();
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            ImageView convertView = new ImageView(mSliderBanner.getContext());
            convertView.setScaleType(ScaleType.FIT_XY);
            Ad item = getItem(position);
            String picUrl = item.getImg();
            convertView.setTag(item);
            if (mClickItemListener != null) {
                convertView.setOnClickListener(mClickItemListener);
                convertView.setTag(R.id.item_key_status,item);
            }
            ImageLoader.getInstance()
                    .displayImage(picUrl, convertView);
            return convertView;
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            int count = mDataList.size();
            if (count > 1) {
                count = Integer.MAX_VALUE;
            }
            return count;
        }

        public int getListSize() {
            return mDataList.size();
        }

        public List<Ad> getList() {
            return mDataList;
        }
    }
}
